package org.tigr.htc.persist;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import org.apache.log4j.Logger;
import org.tigr.htc.common.HTCConfig;
import org.tigr.htc.common.HTCConfigException;

/**
 * The <b>PersistFactory</b> class can be used to instantiate a particular type of persistence 
 * class. The class reads the type of persistence store from the config file by reading the 
 * 'persist' property and instantiates that particular type of class.
 */
public class PersistFactory {
    static Logger log = Logger.getLogger(PersistFactory.class);
    /**
     * The variable <code>pl</code> holds the instance of the persist class
     */
    static private IPersist pl = null;
    /**
     * The variable <code>persistType</code> holds the type of persistence store
     */
    static private String persistType = HTCConfig.getProperty("persist");
    static public final String HASH = "hash";
    static public final String CASTOR = "castor";
    
    private PersistFactory( ) { }
    
    /**
     * The method <code>getIPersist</code> returns the singleton instance of the persistence
     * layer class used by the server. If the class does not already exist, and instance is first
     * created.
     * 
     * @return a <code>IPersist</code> class that can be used to persist the data to the
     * data store
     * 
     * @throws HTCConfigException a <code></code>
     */
    public synchronized static IPersist getIPersist() 
    throws HTCConfigException {
        
        if (pl == null) {
            log.info("Persist type is " + persistType);
            if ((persistType == null) || (persistType.equals(PersistFactory.HASH))) {
                log.info("Creating a HASH persist layer");
            } 
            else if(persistType.equals(PersistFactory.CASTOR)) {
                log.info("creating a CASTOR persist layer");
                pl = (IPersist) new CastorJDO();
            } 
            else {
                log.fatal("Unknown persist layer " + persistType);
                throw new HTCConfigException("Unknown persist layer " + persistType);
            }
        }
        
        return pl;
    }
    
}
